
// INCLUDES ********************************************************************

#include "EAGLViewController.h"

#include "Configuration.h"

#include "EAGLView.h"



// NAMESPACES ******************************************************************

// FORWARD DECLARATIONS ********************************************************

// *****************************************************************************

#pragma mark -
#pragma mark Private methods
// -----------------------------------------------------------------------------

@interface EAGLViewController ()

- ( void)setScreenSizeForRotation:( UIInterfaceOrientation) orientation;

@end

#pragma mark -
#pragma mark Implementation
// -----------------------------------------------------------------------------

@implementation EAGLViewController

- ( id)init
{
    self = [super init];
    
    if ( self == nil)
    {
        return nil;
    }
    
    mView = nil;
    
    mShouldAutorotate = YES;
    
    return self;
}

// -----------------------------------------------------------------------------

- ( void)dealloc
{
    [super dealloc];
}

#pragma mark -
#pragma mark OpenGL view
// -----------------------------------------------------------------------------

- ( EAGLView*)defaultViewForOrientations:( std::vector<UIInterfaceOrientation>) orientations
{
    if ( mView != nil)
    {
        return mView;
    }
    
    std::vector<UIInterfaceOrientation>::const_iterator entry = orientations.begin();
    
    while ( entry != orientations.end())
    {
        mSupportedOrientations.insert( *entry);
        
        entry++;
    }
    
    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    mView = [[EAGLView alloc] initWithFrame:screenBounds
                                orientation:orientations[0]
                                 andOptions:nil
            ];
    
    self.view = mView;
    
    [self setScreenSizeForRotation:orientations[0]];
    
    [mView adaptOrientation:orientations[0]];
    
    return mView;
}

#pragma mark -
#pragma mark Interface orientation
// -----------------------------------------------------------------------------

- ( BOOL)shouldAutorotate
{
    return mShouldAutorotate;
}

// -----------------------------------------------------------------------------

- ( void)setShouldAutorotate:( BOOL)shouldAutorotate
{
    mShouldAutorotate = shouldAutorotate;
}

// -----------------------------------------------------------------------------

- ( BOOL)shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation)orientation
{
    if ( mSupportedOrientations.find( orientation) != mSupportedOrientations.end())
    {
        return YES;
    }

    return NO;
}

// -----------------------------------------------------------------------------

- ( void)willRotateToInterfaceOrientation:( UIInterfaceOrientation)orientation 
                                 duration:( NSTimeInterval)duration
{
    if ( mView == nil)
    {
        return;
    }
    
    [self setScreenSizeForRotation:orientation];
    
    [mView adaptOrientation:orientation];

    Meteor::Configuration::Instance().SetValueForKey<int>( "Current interface orientation",
                                                           orientation);
}

// -----------------------------------------------------------------------------

- ( void)didRotateFromInterfaceOrientation:( UIInterfaceOrientation)orientation
{
}

// -----------------------------------------------------------------------------

- ( void)setScreenSizeForRotation:( UIInterfaceOrientation) orientation
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    
    Meteor::Vector2D screenSize;
    
    if ( orientation == UIInterfaceOrientationLandscapeLeft ||
         orientation == UIInterfaceOrientationLandscapeRight)
    {
        screenSize.SetX( screenBounds.size.height);
        screenSize.SetY( screenBounds.size.width);
    }
    else
    {
        screenSize.SetX( screenBounds.size.width);
        screenSize.SetY( screenBounds.size.height);
    }
    
    Meteor::Configuration::Instance().SetValueForKey<Meteor::Vector2D>( "Screensize",
                                                                        screenSize);
}

@end
